import React from 'react';
import PropTypes from 'prop-types';
import {compose} from 'redux';
import {stripesConnect} from '@folio/stripes/core';
import BooInStorageDetail from '../views/BookInStorageDetail/BookInStorageDetail';

class BookInStorageRecordContainer extends React.Component {
  static manifest = Object.freeze({
    query: {},
    permUserId: {},   // ID of the current permissions user record (see BookInStorageEdit.js)
    selBookInStorage: {
      type: 'okapi',
      path: 'bookInStorages/:{id}',
      clear: false,
      shouldRefresh: (resource, action, refresh) => {
        const { path } = action.meta;
        return refresh || (path && path.match(/link/));
      },
    },
    submitBookInStorage: {
      type: 'okapi',
      // throwErrors: false,
      fetch: false,
      POST: {
        path: 'bookInStorages/submit/:{id}',
      },
    },
    records: {
      type: 'okapi',
      records: 'bookInStorages',
      path: 'bookInStorages',
      fetch: false,
    },
  });

  static propTypes = {
    stripes: PropTypes.shape({
      hasPerm: PropTypes.func.isRequired,
      connect: PropTypes.func.isRequired,
      locale: PropTypes.string.isRequired,
      logger: PropTypes.shape({
        log: PropTypes.func.isRequired,
      }).isRequired,
    }).isRequired,
    resources: PropTypes.shape({
      selBookInStorage: PropTypes.object,
      query: PropTypes.object,
    }),
    mutator: PropTypes.shape({
      selBookInStorage: PropTypes.shape({
        PUT: PropTypes.func.isRequired,
      }),
      submitBookInStorage: PropTypes.shape({
        POST: PropTypes.func.isRequired,
      }),
    }),
    match: PropTypes.shape({
      path: PropTypes.string.isRequired,
      params: PropTypes.shape({
        id: PropTypes.string,
      }),
    }).isRequired,
    okapi: PropTypes.object,
    children: PropTypes.node,
  };

  render() {
    const { children, ...rest } = this.props;
    if (typeof children === 'function') {
      return children({ source: this.source, ...rest });
    }

    return (<BooInStorageDetail {...this.props}>{children}</BooInStorageDetail>);
  }
}

export default compose(
  stripesConnect
)(BookInStorageRecordContainer);
